<template>
  <div class="my-goods-item">
    <div class="left">
      <div class="custom-control custom-checkbox">
        <input
          type="checkbox"
          class="custom-control-input"
          :id="'input' + id"
          :checked="state"
          @change="stateChange"
        />
        <label class="custom-control-label" for="'input' + id">
          <img :src="pic" alt="" />
        </label>
      </div>
    </div>
    <div class="right">
      <div class="top">{{ top }}</div>
      <div class="bottom">
        <span class="price">¥{{ price }}</span>
      <count :num="count" :id='id'></count>
      </div>
    </div>
  </div>
</template>

<script>
import Count from '../MyCount/index.vue'
export default {
  props: {
    // 商品的 id
    // 为啥在这里要封装一个 id 属性呢?
    // 原因:将来,子组件中商品的勾选状态变化之后,需要通过子 -> 父的形式,
    // 通知父组件根据 id 修改对应商品的勾选状态
    id: {
      required: true,
      type: Number
    },
    // 要渲染的商品的名字
    top: {
      default: '',
      type: String
    },
    // 要渲染的商品的图片
    pic: {
      default: '',
      type: String
    },
    // 要渲染的商品的价格
    price: {
      // eslint-disable-next-line vue/require-valid-default-prop
      default: '',
      type: Number
    },
    // 商品的勾选状态
    state: {
      default: true,
      type: Boolean
    },
    // 商品的购买数量
    count: {
      type: Number,
      default: 1
    }
  },
  methods: {
    stateChange (e) {
      const newState = e.target.checked
      // 触发自定义状态
      this.$emit('state-change', { id: this.id, value: newState })
    }
  },
  components: {
    Count
  }
}
</script>

<style lang="less" scoped>
.my-goods-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .left {
    img {
      width: 120px;
      height: 120px;
      margin-right: 8px;
      border-radius: 10px;
    }
    .custom-control-label::before,
    .custom-control-label::after {
      top: 50px;
    }
  }
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .top {
      font-size: 14px;
      font-weight: 700;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      padding: 5px 0;
      align-items: center;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
}
</style>
